import { downloadFile, queryUrlParams } from '@/utils/index'
import request from '@/utils/request'

const _addList = function(data) {
  return request({
    url: 'drug/addList',
    method: 'post',
    data
  })
}

export function add(data, node) {
  var dList = []
  var list1 = node.ruleForm.list1
  var list2 = node.ruleForm.list2
  var list3 = node.ruleForm.list3
  var form1 = node.$refs.form3.form
  var form2 = node.$refs.form4.form
  var form3 = node.$refs.form5.form

  list1.forEach(item => {
    if (item.drugName.length > 0) {
      dList.push(item)
    }
  })
  list2.forEach(item => {
    if (item.drugName.length > 0) {
      dList.push(item)
    }
  })
  list3.forEach(item => {
    if (item.drugName.length > 0) {
      dList.push(item)
    }
  })
  if (form1.drugName.length > 0) {
    form1.drugType = 3
    dList.push(form1)
  }
  if (form2.drugName.length > 0) {
    form2.drugType = 4
    dList.push(form2)
  }
  if (form3.drugName.length > 0) {
    form3.drugType = 5
    dList.push(form3)
  }

  return new Promise((resolve, reject) => {
    const res = _addList(dList)
    res.then(res => {
      node.resetForm()
      node.$parent.$parent.searchCountByDrugType()
      resolve(res)
    }).catch(err => {
      reject(err)
    })
  })
}

export function edit(e, node) {
  const param = node.getParam()
  let data = param.data
  switch (param.type) {
    case 1:
    case 2:
    case 6:
      data = data[0]
      break
  }
  return new Promise((resolve, reject) => {
    request({
      url: 'drug/update',
      method: 'post',
      data: data
    }).then(res => {
      node.$parent.$parent.searchCountByDrugType()
      resolve(res)
    }).catch(err => {
      reject(err)
    })
  })
}

/**
 *
 * @param {*} type 0-修改库位号，1-修改价格,2-修改预警值
 * @param {*} data
 * @returns
 */
export function columnEdit(type, data) {
  let url = 'drug/updateDrugNo'

  switch (type) {
    case 1:
      url = 'drug/updatePrice'
      break
    case 2:
      url = 'drug/batchUpdateWarning'
      break
  }

  return request({
    method: 'post',
    data,
    url
  })
}

export function getDrugTypes() {
  return request({
    url: 'drug/getDrugTypes',
    method: 'post'
  })
}

export function getCountByDrugType(data) {
  const url = 'drug/getCountByDrugType?status=0&pharmacyId=' + data.pharmacyId
  return request({
    url: url,
    method: 'get'
  })
}

export function editStatus(row) {
  const data = {
    id: row.id,
    status: row.status,
    warnNum: row.warnNum
  }
  return request({
    url: 'drug/updateStatus',
    method: 'post',
    data
  })
}

export function getDetails(data) {
  const param = queryUrlParams(data)
  return request({
    url: 'drug/getDetails?' + param,
    method: 'get'
  })
}

export function updateDrugDetail(data) {
  const postData = {
    batchNo: data.batchNo,
    costPrice: data.costPrice,
    id: data.id,
    termOfValidity: data.termOfValidity
  }
  return request({
    url: 'drug/updateDrugDetail',
    method: 'post',
    data: postData
  })
}

export function batchUpdateWarning(data) {
  return request({
    url: 'drug/batchUpdateWarning',
    method: 'post',
    data
  })
}

export function batchUpdateStatus(data) {
  return request({
    url: 'drug/batchUpdateStatus',
    method: 'post',
    data
  })
}

export function downloadDrugByType(data) {
  return request({
    url: 'drug/downloadDrugByType',
    method: 'post',
    responseType: 'blob',
    data
  }).then(result => {
    downloadFile(result, '药品目录', 'xlsx')
  })
}

export function getStockDetails(data) {
  const param = queryUrlParams(data)
  return request({
    url: 'drug/getStockDetails?' + param,
    method: 'get'
  })
}

export function outStockDetails(data) {
  const param = queryUrlParams(data)
  return request({
    url: 'drug/outStockDetails?' + param,
    method: 'get',
    responseType: 'blob'
  }).then(result => {
    downloadFile(result, '药品使用明细', 'xlsx')
  })
}

export function outDrugs(data) {
  const param = queryUrlParams(data)
  return request({
    url: 'drug/outDrugs?' + param,
    method: 'get',
    responseType: 'blob'
  }).then(result => {
    downloadFile(result, '库存', 'xlsx')
  })
}

/**
 * 导出批次详情
 * @param {*} data
 * @returns
 */
export function outDetails(data) {
  const param = queryUrlParams(data)

  return new Promise((resolve, reject) => {
    request({
      url: 'drug/outDetails?' + param,
      method: 'get',
      responseType: 'blob'
    }).then(result => {
      resolve()
      downloadFile(result, '批次详情', 'xlsx')
    }).catch(error => {
      reject(error)
    })
  })
}

export default { add, edit, getDrugTypes, getCountByDrugType, columnEdit }
